# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ruby-ffi
_gemname=ffi
pkgver=1.13.1
pkgrel=0
pkgdesc="Ruby FFI library"
url="https://github.com/ffi/ffi"
arch="all"
license="BSD-3-Clause"
checkdepends="ruby-bigdecimal ruby-fiddle ruby-rspec"
makedepends="libffi-dev ruby ruby-dev"
install="$pkgname.post-install"
source="$pkgname-$pkgver.tar.gz::https://github.com/ffi/$_gemname/archive/$pkgver.tar.gz
	gemspec.patch"
builddir="$srcdir/$_gemname-$pkgver"

build() {
	gem build $_gemname.gemspec
	gem install --local \
		--install-dir dist \
		--ignore-dependencies \
		--no-document \
		--verbose \
		$_gemname-$pkgver.gem
}

check() {
	local exclude=''
	# FIXME fails with: expected -0.477986771... to be within 0.01 of 0.3
	case "$CARCH" in
		x86)  exclude='spec/ffi/long_double_spec.rb' ;;
		mips*)  exclude='spec/ffi/long_double_spec.rb' ;;
	esac

	GEM_PATH='dist:.gem' rspec --exclude-pattern "$exclude"
}

package() {
	local gemdir="$pkgdir/$(ruby -e 'puts Gem.default_dir')"

	local carch
	case "$CARCH" in
		x86) carch="i386";;
		arm*) carch="arm";;
		ppc64le) carch="powerpc64";;
		*) carch="$CARCH";;
	esac

	cd "$builddir"/dist

	mkdir -p "$gemdir"
	cp -r extensions gems specifications "$gemdir"/

	# Remove unnecessary files and rubbish...
	find "$gemdir"/extensions/ -name mkmf.log -delete

	cd "$gemdir"/gems/$_gemname-$pkgver
	rm -rf ext/ lib/*.so
	find lib/ffi/platform/* -prune ! -name "$carch-linux" -exec rm -rf {} +
}

sha512sums="f0077518c9ced54881e40a0047440edb4e40ba0ba89fc814a489bcbaf52e08e9a773e28f453f579162439e247151b994936695fe0b1ed5fb429d9bfd85b845ef  ruby-ffi-1.13.1.tar.gz
4ed0d22ad42dd6b3a20532af09bca0501c7d90fb087d49f7b8e537579aa98ab42bfc21d0f647b601524d72e8187327780b4ee45208d36b6a6507d2cc1b0b7cc2  gemspec.patch"
